package com.arpa.wms.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.arpa.ntocc.common.common.cache.PartyCache;
import com.arpa.ntocc.common.common.exception.ServiceException;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.wms.common.util.OrderIdGeneratorUtil;
import com.arpa.wms.domain.consts.NumConst;
import com.arpa.wms.domain.entity.*;
import com.arpa.wms.domain.enums.PickingStatusEnum;
import com.arpa.wms.mapper.DeliveryMapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.taobao.pac.sdk.cp.dataobject.request.TMS_TRUNK_ORDER_NOTIFY.DeliveryService;
import lombok.extern.log4j.Log4j2;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

import static com.arpa.wms.domain.consts.NumConst.NUM_ONE;
import static com.arpa.wms.domain.consts.NumConst.NUM_ZERO;
import static com.arpa.wms.service.impl.DeliveryServiceImpl.*;

/**
 * @author liuxr
 * @date 2021年12月09日 9:05
 */
@Service
@Log4j2
public class WaveService {

    @Resource
    private DeliveryMapper deliveryMapper;

    @Resource
    private PartyCache partyCache;

    private final IInventoryService inventoryService;

    private final IPickingService pickingService;

    private IPickingItemService iPickingItemService;

    private IPickingRegisterService IPickingRegisterService;

    private IDeliveryService iDeliveryService;

    private IDeliveryItemService iDeliveryItemService;

    private IDeliveryDistributionService iDeliveryDistributionService;



    public WaveService(IInventoryService inventoryService,
                       IPickingService pickingService,IPickingItemService iPickingItemService,IPickingRegisterService IPickingRegisterService,IDeliveryService iDeliveryService,IDeliveryItemService iDeliveryItemService
    ,IDeliveryDistributionService iDeliveryDistributionService) {
        this.inventoryService = inventoryService;
        this.pickingService = pickingService;
        this.iPickingItemService = iPickingItemService;
        this.IPickingRegisterService = IPickingRegisterService;
        this.iDeliveryService = iDeliveryService;
        this.iDeliveryItemService = iDeliveryItemService;
        this.iDeliveryDistributionService = iDeliveryDistributionService;
    }

    /**
     * 生成波次
     *
     * @param codes            订单号
     * @param maxOrderQuantity 单波次最大订单数
     * @param temporaryCode    临时波次标识
     * @param isReview         是否复核：0不复核，1复核
     */
    public void genWave(List<String> codes, Integer maxOrderQuantity, String temporaryCode, int isReview) {
        String groupCode = UserUtil.getBranchCode();
        log.info("生成波次，订单号:{}，单波次最大订单数:{}，临时波次标识：{}，是否复核（0不复核，1复核）：{}", codes,maxOrderQuantity,temporaryCode,isReview);
        //获取货主、承运商分组信息，通常是一组一个波次 当前未取消订单
        // Map[shipmentCode,logisticsCode,prepackagedCode,count]
        List<Map<String, Object>> waveGroupInfoMap = this.deliveryMapper.queryWaveGroupInfo(codes,groupCode);
        if (IterUtil.isEmpty(waveGroupInfoMap)) {
            log.error("生成波次找不到符合条件的订单信息，订单号:{}", codes);
            throw new ServiceException("生成波次找不到符合条件的订单信息，订单号:" + codes);
        }
        log.info("生成波次，订单号:{}，按货主、承运商分组订单信息：{}", codes,JSONUtil.toJsonStr(waveGroupInfoMap));
        List<Map<String, Object>> mapList;
        List<Integer> failCodes = Lists.newArrayList();
        Map<Integer, Future<Boolean>> futureMap = Maps.newHashMap();
        Map<Integer, Map<String, Object>> allMap = Maps.newHashMap();
        StringBuilder failMsg = new StringBuilder();

        if (null != maxOrderQuantity && maxOrderQuantity > 0) {
            List<Map<String, Object>> temp = waveGroupInfoMap.stream()
                    .filter(g -> maxOrderQuantity >= MapUtil.getLong(g, COUNT))
                    .collect(Collectors.toList());
            mapList = getMaps(temporaryCode, temp);
            //订单数量大于最大订单波次数量
            List<Map<String, Object>> map = waveGroupInfoMap.stream()
                    .filter(g -> maxOrderQuantity < MapUtil.getLong(g, COUNT)).collect(Collectors.toList());
            if (IterUtil.isNotEmpty(map)) {
                for (Map<String, Object> m : map) {
                    //总记录数
                    long count = MapUtil.getLong(m, COUNT);
                    //总页数
                    long totalpage = (count + (maxOrderQuantity - 1)) / maxOrderQuantity;
                    //TODO 后期改成多线程
                    for (int i = 0; i < totalpage; i++) {
                        Map<String, Object> tmp = new HashMap<>();
                        tmp.put(SHIPMENT_CODE, MapUtil.getStr(m, SHIPMENT_CODE));
                        tmp.put(LOGISTICS_CODE, MapUtil.getStr(m, LOGISTICS_CODE));
                        tmp.put(PREPACKAGEDCODE, MapUtil.getInt(m, PREPACKAGEDCODE));
                        tmp.put(TEMPORARY_CODE, temporaryCode);
                        tmp.put(PAGE_NUM, i);
                        tmp.put(PAGE_SIZE, maxOrderQuantity);
                        mapList.add(tmp);
                    }
                }
            }
        } else {
            mapList = getMaps(temporaryCode, waveGroupInfoMap);
        }
        log.info("生成波次，临时波次标识:{}，波次生成元数据:{}", temporaryCode, mapList);
        if (IterUtil.isNotEmpty(mapList)) {
            for (Map<String, Object> m : mapList) {
                allMap.put(m.hashCode(), m);
                //入驻商
                String shipmentCode = MapUtil.getStr(m, SHIPMENT_CODE);
                //承运商
                String logisticsCode = MapUtil.getStr(m, LOGISTICS_CODE);
                //是否预打包订单 0-否 1-是
                String prepackagedCode = MapUtil.getStr(m, PREPACKAGEDCODE);
                Integer pageNum = (Integer) m.get(PAGE_NUM);
                Integer pageSize = (Integer) m.get(PAGE_SIZE);
                Future<Boolean> future = runAsyncGenWave(shipmentCode, logisticsCode, temporaryCode,prepackagedCode, isReview, pageNum, pageSize);
                if (future == null) {
                    failCodes.add(m.hashCode());
                } else {
                    futureMap.put(m.hashCode(), future);
                }
            }
            futureMap.forEach((key, future) -> {
                try {
                    while (true) {
                        if (future.isDone()) {
                            Boolean status = future.get();
                            if (!status) {
                                failCodes.add(key);
                            }
                            break;
                        }
                    }
                } catch (ServiceException | InterruptedException | ExecutionException e) {
                    log.error("生成波次【{}】线程获取结果【{}】", key, e.getMessage());
                    failMsg.append("生成波次:").append(key).append("异常信息:").append(e.getMessage()).append(";");
                    failCodes.add(key);
                    Thread.currentThread().interrupt();
                }
            });
            if (IterUtil.isNotEmpty(failCodes)) {
                throw new ServiceException(failMsg.toString());
            }
        } else {
            log.error("没有找到要生成波次的订单数据，临时波次标识：{}；", temporaryCode);
            throw new ServiceException("没有要生成波次的订单数据");
        }
    }
    /**
     * 多线程异步创建波次
     *
     * @param shipmentCode
     * @param logisticsCode
     * @param temporaryCode
     * @param isReview      是否复核：0不复核，1复核
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Async
    @Transactional(rollbackFor = Exception.class)
    public Future<Boolean> runAsyncGenWave(
            String shipmentCode,
            String logisticsCode,
            String temporaryCode,
            String prepackagedCode,
            int isReview,
            Integer pageNum,
            Integer pageSize) {
        runWave(shipmentCode, logisticsCode, temporaryCode,prepackagedCode, isReview, pageNum, pageSize);
        return new AsyncResult<>(true);
    }

    /**
     * 生成波次数据
     * @author liuxr
     * @date 2021/11/24 15:19
     * @param shipmentCode 货主标识
     * @param logisticsCode 承运商标识
     * @param temporaryCode 临时波次
     * @param prepackagedCode
     * @param isReview
     * @param pageNum
     * @param pageSize
     */
    public void runWave(
            String shipmentCode,
            String logisticsCode,
            String temporaryCode,
            String prepackagedCode,
            int isReview,
            Integer pageNum,
            Integer pageSize) throws ServiceException {
        String userCode = UserUtil.getCode();
        String groupCode = UserUtil.getBranchCode();
        String userName = partyCache.translate(userCode);
        //分组获取订单信息
        List<String> deliveryCodesList = deliveryMapper.getGenWaveDeliveryCodes(shipmentCode, logisticsCode, temporaryCode,prepackagedCode, pageNum, pageSize);
        if (IterUtil.isEmpty(deliveryCodesList)) {
            log.error("找不到订单信息，入驻商:{},承运商:{},临时波次标识:{}", shipmentCode, logisticsCode, temporaryCode);
            throw new ServiceException("找不到订单信息，入驻商:" + shipmentCode + ",承运商:" + logisticsCode + ",临时波次标识:" + temporaryCode);
        }
        //获取发货单分配明细按库存分组
        List<Map<String, Object>> distributionMap = this.deliveryMapper.getDeliveryDistributionWaveGroupInfo(deliveryCodesList,groupCode);
        if (IterUtil.isEmpty(distributionMap)) {
            log.error("找不到订单分配信息，入驻商:{},承运商:{},临时波次标识:{}", shipmentCode, logisticsCode, temporaryCode);
            throw new ServiceException("找不到订单分配信息，入驻商:" + shipmentCode + ",承运商:" + logisticsCode + ",临时波次标识:" + temporaryCode);
        }
        log.info("生成波次，入驻商:{},承运商:{},临时波次标识:{}，获取发货单分配明细按库存分组:{}", shipmentCode, logisticsCode, temporaryCode, distributionMap);

        BigDecimal distributionNumSum = distributionMap.stream().map(d->MapUtil.get(d, "distributionQuantitySum",BigDecimal.class)).reduce(BigDecimal.ZERO,BigDecimal::add);
        //生成波次拣货单号
        String wavePickingCode = OrderIdGeneratorUtil.generatorOrderId(OrderIdGeneratorUtil.OrderTypeEnum.BC, shipmentCode,groupCode);
        log.info("生成波次，入驻商:{},承运商:{},临时波次标识:{}，生成波次号:{}", shipmentCode, logisticsCode, temporaryCode, wavePickingCode);
        /******生成拣货单信息*******/
        //生成拣货单号
        String pickingCode = OrderIdGeneratorUtil.generatorOrderId(OrderIdGeneratorUtil.OrderTypeEnum.PK, shipmentCode,groupCode);
        log.info("生成波次，入驻商:{},承运商:{},临时波次标识:{}，生成拣货单号:{}", shipmentCode, logisticsCode, temporaryCode, pickingCode);
        Delivery delivery = deliveryMapper.selectOne(Wrappers.lambdaQuery(Delivery.class)
                .select(Delivery::getGroupCode,Delivery::getWarehouseCode,Delivery::getShipmentCode,Delivery::getShipmentName,
                        Delivery::getType,Delivery::getCarrierCode,Delivery::getCarrierName,Delivery::getLogisticsCode)
                .eq(Delivery::getCode,deliveryCodesList.get(0))
                .eq(Delivery::getGroupCode,groupCode));
        Picking picking = new Picking();
        BeanUtil.copyProperties(delivery, picking);
        picking.setCode(pickingCode);
        picking.setReview(isReview);
        picking.setLogisticsCode(logisticsCode);
        picking.setTemporaryCode(temporaryCode);
        picking.setStockoutQuantity(BigDecimal.ZERO);
        picking.setWavePickingCode(wavePickingCode);
        picking.setNotPickingQuantity(distributionNumSum);
        picking.setPlanQuantity(distributionNumSum);
        picking.setStatus(PickingStatusEnum.NOT_PICK.getValue());
        picking.setCreatedBy(userCode);
        picking.setCreatedName(userName);
        picking.setGmtCreated(LocalDateTime.now());
        picking.setModifiedBy(userCode);
        picking.setModifiedName(userName);
        picking.setGmtModified(LocalDateTime.now());
        picking.setPrepackagedCode(prepackagedCode);
        if (StrUtil.isNotEmpty(prepackagedCode)) {
            picking.setPrepackaged(NUM_ONE);
        } else {
            picking.setPrepackaged(NUM_ZERO);
        }
        //波次商品种类数量
        int goodsSortQuantity =  deliveryMapper.getGoodsSortQuantity(deliveryCodesList,groupCode);
        picking.setGoodsSortQuantity(  BigDecimal.valueOf(goodsSortQuantity));

        /******生成拣货单明细，拣货登记明细*******/
        List<PickingItem> pickingItemList = new ArrayList<>();
        List<PickingRegister> pickingRegisterList = new ArrayList<>();
        List<String> oldInventoryCodes = distributionMap.stream().map(r-> MapUtil.getStr(r, "oldInventoryCode")).collect(Collectors.toList());
        List<Inventory> inventoryList = inventoryService.list(Wrappers.lambdaQuery(Inventory.class)
                .select(Inventory::getCode,Inventory::getGoodsCode,Inventory::getGoodCode,Inventory::getGoodsName,
                        Inventory::getGoodsBarCode,Inventory::getSpec,Inventory::getExpirationQuantity,
                        Inventory::getGoodsUnit,Inventory::getUnitName,Inventory::getLot,
                        Inventory::getReceiveLot,Inventory::getGmtManufacture,Inventory::getGmtExpire,
                        Inventory::getGmtStock,Inventory::getSupplier,Inventory::getExtendOne,
                        Inventory::getExtendTwo,Inventory::getExtendThree,Inventory::getExtendFive,
                        Inventory::getExtendFive,Inventory::getExtendSix,Inventory::getGoodsStatus,
                        Inventory::getGoodsStatusName,Inventory::getWarehouseCode,Inventory::getWarehouseSpaceCode,
                        Inventory::getLocation,Inventory::getIsLock,Inventory::getContainerBarCode)
                .in(Inventory::getCode,oldInventoryCodes));
        Map<String,Inventory> inventoryMap = inventoryList.stream().collect(Collectors.toMap(Inventory::getCode,r->r));
        for (Map<String, Object> d : distributionMap) {
            //临时库存标识
            String inventoryCode = MapUtil.getStr(d, "inventoryCode");
            //原库存标识
            String oldInventoryCode = MapUtil.getStr(d, "oldInventoryCode");
            //分配总数量
            String distributionQuantitySum = MapUtil.getStr(d, "distributionQuantitySum");
            Inventory inventory = MapUtil.get(inventoryMap,oldInventoryCode,Inventory.class);
            PickingItem pickingItem = new PickingItem();
            BeanUtil.copyProperties(inventory, pickingItem);
            pickingItem.setCode(IdUtil.simpleUUID());
            pickingItem.setGroupCode(groupCode);
            pickingItem.setTemporaryCode(temporaryCode);
            pickingItem.setPickingCode(picking.getCode());
            pickingItem.setDeliveryItemCode(StrUtil.EMPTY);
            pickingItem.setGoodsUnitName(inventory.getUnitName());
            pickingItem.setStockoutQuantity(BigDecimal.ZERO);
            pickingItem.setInventoryCode(inventoryCode);
            pickingItem.setOldInventoryCode(oldInventoryCode);
            pickingItem.setDistributionQuantity(new BigDecimal(distributionQuantitySum));
            pickingItem.setPlanQuantity(new BigDecimal(distributionQuantitySum));
            // 待拣货状态
            pickingItem.setStatus(PickingStatusEnum.NOT_PICK.getValue());
            pickingItem.setWavePickingCode(wavePickingCode);
            pickingItem.setCreatedBy(userCode);
            pickingItem.setCreatedName(userName);
            pickingItem.setGmtCreated(LocalDateTime.now());
            PickingRegister pickingRegister = new PickingRegister();
            BeanUtil.copyProperties(pickingItem, pickingRegister);
            pickingRegister.setCode(IdUtil.simpleUUID());
            pickingRegister.setPickingItemCode(pickingItem.getCode());
            pickingRegister.setNotPickingQuantity(pickingRegister.getDistributionQuantity());
            pickingRegister.setBasicQuantity(BigDecimal.ONE);
            pickingRegister.setTemporaryCode(temporaryCode);
            pickingItemList.add(pickingItem);
            pickingRegisterList.add(pickingRegister);
        }
        //由于用到订单的波次号，所以需要先更新订单信息，然后生成商品订单标识
        String orderTag = generateOrderTag(deliveryCodesList,groupCode);
        picking.setOrderTag(orderTag);
        List<Picking> pickingList = new ArrayList<>();
        pickingList.add(picking);
        //批量保存拣货单，拣货明细，拣货登记信息
        saveData(wavePickingCode,deliveryCodesList,pickingList,pickingItemList, pickingRegisterList,groupCode);

    }

    @Transactional(rollbackFor = {Exception.class})
    public void saveData(String wavePickingCode,List<String> deliveryCodes, List<Picking> pickingList, List<PickingItem> pickingItemList, List<PickingRegister> pickingRegisterList,String groupCode) {
        //更新订单状态
        log.info("生成波次，更新订单、订单明细、订单分配明细待拣货状态和波次号：{},订单号：{}", wavePickingCode, JSONUtil.toJsonStr(deliveryCodes));
        this.deliveryMapper.updateGenWaveDeliveryWavePickingCode(wavePickingCode,deliveryCodes,groupCode);
        //批量保存拣货单，拣货明细，拣货登记信息
        pickingService.savePickingsInfo(pickingList,pickingItemList, pickingRegisterList);
    }





    /**
     * 生成订单标识
     *
     * @return
     */
    private String generateOrderTag(List<String> deliveryCodes,String groupCode) {
        String orderTag = "";
        //订单商品种数分组
        List<Map<String, Object>> mapList = getDeliveryItemGoodsSortQuantity(deliveryCodes,groupCode);
        //分组后数据只有一条，则为爆品
        if (mapList.size() == NumConst.NUM_ONE) {
            //爆品
            orderTag = "4";
        }
        if (mapList.size() > NumConst.NUM_ONE) {


           int multiDeliveryCount =  deliveryMapper.selectCount(Wrappers.lambdaQuery(Delivery.class)
                   .in(Delivery::getCode, deliveryCodes)
                   .eq(Delivery::getOrderTag,"多品")
                   .eq(Delivery::getGroupCode,groupCode)
           );
            //多品业务
            if (multiDeliveryCount>NUM_ZERO) {
                //查询总的订单
                List<Delivery> multiDeliveryList = deliveryMapper.selectList(Wrappers.lambdaQuery(Delivery.class)
                        .select(Delivery::getCode,
                                Delivery::getGoodsSortQuantity,
                                Delivery::getDistributionNum,
                                Delivery::getOrderTag)
                        .in(Delivery::getCode, deliveryCodes)
                        .eq(Delivery::getGroupCode,groupCode)
                );
                //订单商品种数分组
                int goodsSortQuantitySize = multiDeliveryList.stream().collect(Collectors.groupingBy(Delivery::getGoodsSortQuantity)).size();
                //订单分配数量分组
                int distributionSize = multiDeliveryList.stream().collect(Collectors.groupingBy(Delivery::getDistributionNum)).size();
                //爆品条件：1.订单的商品种类和数量都对应一致
                if (goodsSortQuantitySize == NumConst.NUM_ONE
                        && distributionSize == NumConst.NUM_ONE
                        && mapList.size() == multiDeliveryList.get(NUM_ZERO).getGoodsSortQuantity().intValue()) {
                    //爆品
                    orderTag = "4";
                } else {
                    //多品
                    orderTag = "3";
                }
            } else {
                int  singleDeliveryNotOneCount =  deliveryMapper.selectCount(Wrappers.lambdaQuery(Delivery.class)
                        .in(Delivery::getCode, deliveryCodes)
                        .eq(Delivery::getOrderTag,"单品")
                        .eq(Delivery::getGroupCode,groupCode)
                        .gt(Delivery::getDistributionNum,NUM_ONE)
                );
                if (singleDeliveryNotOneCount>0) {
                    //单品多件
                    orderTag = "2";
                } else {
                    orderTag = "1";
                }
            }
        }
        return orderTag;
    }


    /**
     * 根据订单明细的分配数量和商品进行分组查询订单明细信息
     *
     * @param deliveryCodes 订单号列表
     * @return
     */
    private List<Map<String, Object>> getDeliveryItemGoodsSortQuantity(List<String> deliveryCodes,String groupCode) {
        return deliveryMapper.getDeliveryItemGoodsSortQuantity(deliveryCodes,groupCode);
    }

    /**
     * 构建要生成波次的条件信息
     *
     * @param temporaryCode 临时波次标识
     * @param temp          订单分组信息
     * @return
     */
    private List<Map<String, Object>> getMaps(String temporaryCode, List<Map<String, Object>> temp) {
        List<Map<String, Object>> mapList;
        mapList = temp.stream().map(m -> {
            Map<String, Object> tmp = new HashMap<>();
            tmp.put(SHIPMENT_CODE, MapUtil.getStr(m, SHIPMENT_CODE));
            tmp.put(LOGISTICS_CODE, MapUtil.getStr(m, LOGISTICS_CODE));
            tmp.put(PREPACKAGEDCODE, MapUtil.getStr(m, PREPACKAGEDCODE));
            tmp.put(TEMPORARY_CODE, temporaryCode);
            return tmp;
        }).collect(Collectors.toList());
        return mapList;
    }

    /**
     * 取消波次功能
     * @param wavePickingCode
     */
    @Transactional(rollbackFor = {Exception.class})
    public void cancelWaveTips(String wavePickingCode) {
        try {

            //删除拣货单  拣货明细单   拣货确认单
//            pickingService.cancelWaveTips(wavePickingCode);
            //更改订单状态
//            deliveryMapper.cancelWaveTips(wavePickingCode);
            //2022-01-17 优化 连表更改表数据过慢进行拆分
            //删除拣货单
            String branchCode = UserUtil.getBranchCode();
            pickingService.update(new UpdateWrapper<Picking>().lambda()
                    .eq(Picking::getWavePickingCode, wavePickingCode)
                    .eq(Picking::getStatus, NumConst.NUM_ZERO)
                    .eq(Picking::getGroupCode,branchCode)
                    .set(Picking::getDeleted, 1));

            //删除明细单
            iPickingItemService.update(new UpdateWrapper<PickingItem>().lambda()
                    .eq(PickingItem::getWavePickingCode, wavePickingCode)
                    .eq(PickingItem::getStatus, NumConst.NUM_ZERO)
                    .eq(PickingItem::getGroupCode,branchCode)
                    .set(PickingItem::getDeleted, 1));

            //删除拣货确认单
            IPickingRegisterService.update(new UpdateWrapper<PickingRegister>().lambda()
                    .eq(PickingRegister::getWavePickingCode, wavePickingCode)
                    .eq(PickingRegister::getStatus, NumConst.NUM_ZERO)
                    .eq(PickingRegister::getGroupCode,branchCode)
                    .set(PickingRegister::getDeleted, 1));

            //更改订单
            iDeliveryService.update(new UpdateWrapper<Delivery>().lambda()
                    .eq(Delivery::getWavePickingCode, wavePickingCode)
                    .eq(Delivery::getStatus, NumConst.NUM_SIX)
                    .eq(Delivery::getGroupCode,branchCode)
                    .set(Delivery::getWavePickingCode, "")
                    .set(Delivery::getOrderNum, 0)
                    .set(Delivery::getStatus, 3));


            //更改订单详情
            iDeliveryItemService.update(new UpdateWrapper<DeliveryItem>().lambda()
                    .eq(DeliveryItem::getWavePickingCode, wavePickingCode)
                    .eq(DeliveryItem::getGroupCode,branchCode)
                    .set(DeliveryItem::getWavePickingCode, "")
                    .set(DeliveryItem::getOrderNum, 0));

            //更改订单分配表
            iDeliveryDistributionService.update(new UpdateWrapper<DeliveryDistribution>().lambda()
                    .eq(DeliveryDistribution::getWavePickingCode, wavePickingCode)
                    .eq(DeliveryDistribution::getGroupCode,branchCode)
                    .set(DeliveryDistribution::getWavePickingCode, "")
                    .set(DeliveryDistribution::getOrderNum, 0));

        }catch (Exception e){
            e.printStackTrace();
            log.error("取消波次异常" + e.getMessage());
        }
    }
}
